package com.parkreport.admin.parkarea.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.common.pager.PageHelper;
import com.common.pager.PagerVO;
import com.common.util.StringUtil;
import com.parkreport.admin.parkarea.service.DmbService;
import com.parkreport.admin.parkarea.service.ParkAreaService;
import com.parkreport.base.BaseAction;
import com.parkreport.vo.ParkArea;
import com.parkreport.vo.ParkDmbVO;

public class ParkAreaAction extends BaseAction {
	private PagerVO pagerVO = null;
	private String ids = null;
	private String id="";
	
	private ParkAreaService service = new ParkAreaService();
	private DmbService dmbService = new DmbService();
	private List<ParkArea> list = new ArrayList<ParkArea>();
	private List<ParkDmbVO> dmbList = new ArrayList<ParkDmbVO>();
	
	private Map<String,String> parkAreaMap = new HashMap<String, String>();
	private ParkArea vo = new ParkArea();
	private String pid = "0";

	public String find(){
		log.info("==========find============");
		PageHelper pager = new PageHelper(this.getRequest());
		
		list = service.find(pid,vo,pager);
		pagerVO = pager.paginate();
		parkAreaMap = service.findMap();
		return topager("/pages/admin/parkarea/list.jsp");
		
	}
	public String findByPid(){
		log.info("==========find============"+pid);
		PageHelper pager = new PageHelper(this.getRequest());
		
		if("0".equals(pid)){
			list = service.find(pid,vo,pager);
			pid="-1";
			id = "0";
		}else{
			list = service.find(pid,vo,pager);
			ParkArea park = service.get(pid);
			pid=park.getPid();
			id = park.getId();
			
		}
		
		pagerVO = pager.paginate();
		parkAreaMap = service.findMap();
		return topager("/pages/admin/parkarea/list.jsp");
		
	}
	
	public String edit(){
		log.info("==========edit============");
		if(!StringUtil.isBlank(id)){
			vo = service.get(id);
			dmbList = dmbService.find(id);
		}
		
		return topager("/pages/admin/parkarea/edit.jsp");
		
	}
	public String add(){
		log.info("==========add============");
		if(!StringUtil.isBlank(pid)){
			vo.setPid(pid);
		}

		return topager("/pages/admin/parkarea/edit.jsp");
		
	}
	public String save(){
		log.info("==========save============"+dmbList.size());
		String isSave = service.save(vo,dmbList);
		
		this.getRequest().setAttribute("saveFlag", isSave);
		return topager("/pages/admin/parkarea/edit.jsp");
	}
	public void delete(){
		log.info("==========delete============"+ids);
		service.delete(ids);
		
	}
	public String getTree(){
		HttpServletResponse response = ServletActionContext.getResponse();
		String treeStr = service.getParkTree();
		
		log.info("treeStr==========="+treeStr);
		try {
			response.getWriter().write(treeStr);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return null;
	}
	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	
	public String getId() {
		return id;
	}

	
	public List<ParkArea> getList() {
		return list;
	}

	public void setList(List<ParkArea> list) {
		this.list = list;
	}

	public ParkArea getVo() {
		return vo;
	}

	public void setVo(ParkArea vo) {
		this.vo = vo;
	}

	public void setId(String id) {
		this.id = id;
	}

	public PagerVO getPagerVO() {
		return pagerVO;
	}

	public void setPagerVO(PagerVO pagerVO) {
		this.pagerVO = pagerVO;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public Map<String, String> getParkAreaMap() {
		return parkAreaMap;
	}

	public void setParkAreaMap(Map<String, String> parkAreaMap) {
		this.parkAreaMap = parkAreaMap;
	}
	public List<ParkDmbVO> getDmbList() {
		return dmbList;
	}
	public void setDmbList(List<ParkDmbVO> dmbList) {
		this.dmbList = dmbList;
	}



	
	
}
